/**
 * @file init_db.py
 * @brief Database initialization script for Meeting Management System.
 * @author Ziyi Wang
 *
 * Creates tables, default admin/test users, and sample meeting rooms.
 */
from app.database import engine, Base, SessionLocal
from app.models.models import User, Room
from app.auth import get_password_hash
from datetime import datetime
import os

/**
 * @brief Initialize the database with tables, admin/test users, and sample rooms.
 * @return None
 */
def init_db():
    # 创建数据库表
    Base.metadata.create_all(bind=engine)
    
    # 创建数据库会话
    db = SessionLocal()
    
    # 检查是否已经有管理员用户
    admin = db.query(User).filter(User.username == "admin").first()
    if not admin:
        # 创建管理员用户
        admin_password = "admin123"  # 在生产环境中应使用更强的密码
        hashed_password = get_password_hash(admin_password)
        admin = User(
            username="admin",
            email="admin@example.com",
            hashed_password=hashed_password,
            full_name="系统管理员",
            is_active=True,
            is_admin=True
        )
        db.add(admin)
        print("已创建管理员用户 (用户名: admin, 密码: admin123)")
    
    # 添加一些测试用户
    test_user = db.query(User).filter(User.username == "test").first()
    if not test_user:
        test_password = "test123"
        hashed_password = get_password_hash(test_password)
        test_user = User(
            username="test",
            email="test@example.com",
            hashed_password=hashed_password,
            full_name="测试用户",
            is_active=True,
            is_admin=False
        )
        db.add(test_user)
        print("已创建测试用户 (用户名: test, 密码: test123)")
    
    # 添加50个各种类型的会议室
    rooms = [
        # 主楼会议室 (1-12)
        {"name": "主楼大会议室A", "capacity": 30, "location": "主楼3层", "facilities": "投影仪、智能白板、视频会议系统、音响系统"},
        {"name": "主楼大会议室B", "capacity": 25, "location": "主楼3层", "facilities": "投影仪、智能白板、视频会议系统、音响系统"},
        {"name": "主楼中会议室A", "capacity": 15, "location": "主楼3层", "facilities": "投影仪、电子白板、视频会议系统"},
        {"name": "主楼中会议室B", "capacity": 12, "location": "主楼2层", "facilities": "投影仪、电子白板、视频会议系统"},
        {"name": "主楼小会议室A", "capacity": 8, "location": "主楼2层", "facilities": "LED电视屏幕、白板、视频会议设备"},
        {"name": "主楼小会议室B", "capacity": 8, "location": "主楼2层", "facilities": "LED电视屏幕、白板"},
        {"name": "主楼小会议室C", "capacity": 6, "location": "主楼1层", "facilities": "LED电视屏幕、无线投屏"},
        {"name": "主楼小会议室D", "capacity": 4, "location": "主楼1层", "facilities": "LED电视屏幕、无线投屏"},
        {"name": "主楼洽谈室A", "capacity": 4, "location": "主楼1层", "facilities": "茶几、沙发、展示屏"},
        {"name": "主楼洽谈室B", "capacity": 4, "location": "主楼2层", "facilities": "茶几、沙发、展示屏"},
        {"name": "主楼董事会议室", "capacity": 20, "location": "主楼5层", "facilities": "高级会议桌椅、投影系统、音响系统、茶水间"},
        {"name": "主楼培训室", "capacity": 40, "location": "主楼4层", "facilities": "讲台、排椅、投影系统、音响系统、麦克风"},
        
        # 科研楼会议室 (13-22)
        {"name": "科研楼会议室A", "capacity": 12, "location": "科研楼5层", "facilities": "投影仪、白板、视频会议设备"},
        {"name": "科研楼会议室B", "capacity": 10, "location": "科研楼3层", "facilities": "LED电视屏幕、电子白板"},
        {"name": "科研楼会议室C", "capacity": 16, "location": "科研楼4层", "facilities": "投影仪、白板、视频会议设备"},
        {"name": "科研楼会议室D", "capacity": 14, "location": "科研楼2层", "facilities": "投影仪、白板、视频会议设备"},
        {"name": "科研楼小型会议室A", "capacity": 5, "location": "科研楼2层", "facilities": "LED电视屏幕、无线投屏"},
        {"name": "科研楼小型会议室B", "capacity": 6, "location": "科研楼3层", "facilities": "LED电视屏幕、无线投屏"},
        {"name": "科研楼研讨室A", "capacity": 8, "location": "科研楼1层", "facilities": "白板墙、创意桌椅"},
        {"name": "科研楼研讨室B", "capacity": 8, "location": "科研楼2层", "facilities": "白板墙、创意桌椅"},
        {"name": "科研楼实验室会议室", "capacity": 10, "location": "科研楼1层", "facilities": "特殊实验桌、投影设备、防护设施"},
        
        # 教学楼会议室 (23-30)
        {"name": "教学楼报告厅", "capacity": 80, "location": "教学楼1层", "facilities": "投影仪、舞台、音响系统、麦克风、灯光系统"},
        {"name": "教学楼中型报告厅", "capacity": 50, "location": "教学楼2层", "facilities": "投影仪、舞台、音响系统、麦克风"},
        {"name": "教学楼会议室A", "capacity": 20, "location": "教学楼2层", "facilities": "投影仪、白板、视频会议系统"},
        {"name": "教学楼会议室B", "capacity": 18, "location": "教学楼3层", "facilities": "投影仪、白板、视频会议系统"},
        {"name": "教学楼会议室C", "capacity": 15, "location": "教学楼3层", "facilities": "投影仪、白板、视频会议系统"},
        {"name": "教学楼研讨室A", "capacity": 12, "location": "教学楼4层", "facilities": "环形桌椅、电子白板"},
        {"name": "教学楼研讨室B", "capacity": 10, "location": "教学楼4层", "facilities": "环形桌椅、电子白板"},
        {"name": "教学楼多媒体教室", "capacity": 30, "location": "教学楼1层", "facilities": "多媒体教学设备、电脑工作站"},
        
        # 特殊功能会议室 (31-40)
        {"name": "视频会议专用室A", "capacity": 16, "location": "主楼4层", "facilities": "高清视频会议系统、环绕音响、触控屏幕、专业摄像头"},
        {"name": "视频会议专用室B", "capacity": 10, "location": "科研楼5层", "facilities": "高清视频会议系统、环绕音响、专业摄像头"},
        {"name": "创意讨论室A", "capacity": 12, "location": "科研楼1层", "facilities": "互动式电子白板、可移动桌椅、创意墙"},
        {"name": "创意讨论室B", "capacity": 10, "location": "主楼1层", "facilities": "互动式电子白板、可移动桌椅、创意墙"},
        {"name": "培训教室A", "capacity": 25, "location": "教学楼3层", "facilities": "投影仪、讲台、电脑桌、培训用电脑、音响系统"},
        {"name": "培训教室B", "capacity": 20, "location": "教学楼2层", "facilities": "投影仪、讲台、电脑桌、培训用电脑、音响系统"},
        {"name": "静音讨论室A", "capacity": 4, "location": "图书馆2层", "facilities": "隔音设施、小型白板、休闲座椅"},
        {"name": "静音讨论室B", "capacity": 6, "location": "图书馆3层", "facilities": "隔音设施、小型白板、休闲座椅"},
        {"name": "远程面试室", "capacity": 2, "location": "行政楼1层", "facilities": "高清摄像头、专业麦克风、隔音设施、优质网络"},
        {"name": "产品展示厅", "capacity": 30, "location": "主楼1层", "facilities": "展示台、投影系统、音响系统、可调节灯光"},
        
        # 行政区会议室 (41-46)
        {"name": "行政会议室A", "capacity": 15, "location": "行政楼2层", "facilities": "投影仪、电子白板、视频会议系统、咖啡机"},
        {"name": "行政会议室B", "capacity": 12, "location": "行政楼3层", "facilities": "投影仪、电子白板、视频会议系统"},
        {"name": "董事会议室A", "capacity": 18, "location": "行政楼3层", "facilities": "高端会议桌椅、投影系统、视频会议系统、茶水服务设备"},
        {"name": "董事会议室B", "capacity": 10, "location": "行政楼4层", "facilities": "高端会议桌椅、投影系统、视频会议系统、茶水服务设备"},
        {"name": "行政小会议室", "capacity": 6, "location": "行政楼2层", "facilities": "圆桌、显示屏、简易茶水设备"},
        {"name": "接待会议室", "capacity": 8, "location": "行政楼1层", "facilities": "豪华沙发、茶几、饮品服务"},
        
        # 休闲会议区 (47-50)
        {"name": "休闲会议区A", "capacity": 6, "location": "主楼1层", "facilities": "沙发座椅、咖啡桌、无线投屏"},
        {"name": "休闲会议区B", "capacity": 8, "location": "科研楼1层", "facilities": "沙发座椅、小型会议桌、LED电视"},
        {"name": "户外会议区", "capacity": 12, "location": "中心花园", "facilities": "遮阳伞、户外桌椅、无线网络"},
        {"name": "咖啡厅会议区", "capacity": 10, "location": "主楼2层", "facilities": "舒适座椅、大桌子、咖啡服务、无线网络"}
    ]
    
    for room_data in rooms:
        room = db.query(Room).filter(Room.name == room_data["name"]).first()
        if not room:
            room = Room(**room_data)
            db.add(room)
            print(f"已创建会议室: {room_data['name']}")
    
    # 提交更改
    db.commit()
    db.close()
    
    print("数据库初始化完成")

if __name__ == "__main__":
    init_db() 